//@version=5
indicator("Breakout Support & Resistance SwiftEdge", overlay=true)

// Inputs
pivot_length = input.int(5, "Pivot Lookback Length", minval=1)
breakout_confirmation = input.int(2, "Breakout Confirmation Bars", minval=1)
tp1_percent = input.float(2.0, "Take Profit 1 (%)", minval=0, step=0.1)
tp2_percent = input.float(4.0, "Take Profit 2 (%)", minval=0, step=0.1)
sl_percent = input.float(1.0, "Stop Loss (%)", minval=0, step=0.1)
show_multiple_entries = input.bool(false, "Show Multiple Entries (up to 5)", tooltip="If true, shows up to 5 recent entries. If false, only shows the latest entry.")
direction_change_threshold = input.float(2.0, "Direction Change Threshold (%)", minval=0, step=0.1, tooltip="Price must move this percentage away from the entry to allow a direction change.")

// Arrays til at gemme de sidste 10 støtte- og modstandsniveauer
var resistance_prices = array.new_float(0)
var resistance_indices = array.new_int(0)
var support_prices = array.new_float(0)
var support_indices = array.new_int(0)

// Arrays til at gemme linjer og labels for støtte og modstand
var resistance_lines = array.new_line(0)
var resistance_labels = array.new_label(0)
var support_lines = array.new_line(0)
var support_labels = array.new_label(0)

// Arrays til at gemme breakout-labels
var breakout_labels = array.new_label(0)

// Arrays til at gemme alle breakouts og deres niveauer
var entry_prices = array.new_float(0)
var entry_indices = array.new_int(0)
var sl_prices = array.new_float(0)
var tp1_prices = array.new_float(0)
var tp2_prices = array.new_float(0)
var is_bullish_trades = array.new_bool(0)

// Arrays til at gemme linjer og labels for entry, stop-loss og take-profit
var entry_lines = array.new_line(0)
var entry_labels = array.new_label(0)
var sl_lines = array.new_line(0)
var sl_labels = array.new_label(0)
var tp1_lines = array.new_line(0)
var tp1_labels = array.new_label(0)
var tp2_lines = array.new_line(0)
var tp2_labels = array.new_label(0)

// Variabler til at spore aktiv handel og retning
var int trade_direction = 0  // 0: ingen handel, 1: bullish, -1: bearish
var float active_entry = na
var float active_sl = na
var float active_tp1 = na
var float active_tp2 = na

// Variabel til at gemme den seneste "Direction Change"-label
var label last_direction_change_label = na

// Identificer støtte- og modstandsniveauer ved hjælp af pivotpunkter
pivot_high = ta.pivothigh(high, pivot_length, pivot_length)
pivot_low = ta.pivotlow(low, pivot_length, pivot_length)

// Gem det seneste pivotniveau i arrays
var float resistance = na
var float support = na

if not na(pivot_high)
    resistance := pivot_high
    array.push(resistance_prices, resistance)
    array.push(resistance_indices, bar_index[pivot_length])
    // Begræns til de sidste 10 niveauer
    if array.size(resistance_prices) > 10
        array.shift(resistance_prices)
        array.shift(resistance_indices)

if not na(pivot_low)
    support := pivot_low
    array.push(support_prices, support)
    array.push(support_indices, bar_index[pivot_length])
    // Begræns til de sidste 10 niveauer
    if array.size(support_prices) > 10
        array.shift(support_prices)
        array.shift(support_indices)

// Slet tidligere linjer og labels for støtte og modstand, hvis arrays ikke er tomme
if array.size(resistance_lines) > 0
    for i = 0 to array.size(resistance_lines) - 1
        line.delete(array.get(resistance_lines, i))
        label.delete(array.get(resistance_labels, i))

if array.size(support_lines) > 0
    for i = 0 to array.size(support_lines) - 1
        line.delete(array.get(support_lines, i))
        label.delete(array.get(support_labels, i))

// Tøm arrays for linjer og labels
array.clear(resistance_lines)
array.clear(resistance_labels)
array.clear(support_lines)
array.clear(support_labels)

// Tegn de sidste 10 støtte- og modstandslinjer, hvis arrays ikke er tomme
if array.size(resistance_prices) > 0
    for i = 0 to array.size(resistance_prices) - 1
        price = array.get(resistance_prices, i)
        idx = array.get(resistance_indices, i)
        new_line = line.new(idx, price, bar_index, price, color=color.new(color.red, 20), style=line.style_dashed, width=2)
        new_label = label.new(idx, price, "Resistance", color=color.new(color.red, 20), style=label.style_label_down, textcolor=color.white)
        array.push(resistance_lines, new_line)
        array.push(resistance_labels, new_label)

if array.size(support_prices) > 0
    for i = 0 to array.size(support_prices) - 1
        price = array.get(support_prices, i)
        idx = array.get(support_indices, i)
        new_line = line.new(idx, price, bar_index, price, color=color.new(color.blue, 20), style=line.style_dashed, width=2)
        new_label = label.new(idx, price, "Support", color=color.new(color.blue, 20), style=label.style_label_up, textcolor=color.white)
        array.push(support_lines, new_line)
        array.push(support_labels, new_label)

// Tjek for breakouts
bullish_breakout = ta.crossover(close, resistance)
bearish_breakout = ta.crossunder(close, support)

// Bekræft breakout med efterfølgende barer
var float breakout_price = na
var bool confirmed_bullish = false
var bool confirmed_bearish = false

bars_since_bullish = ta.barssince(bullish_breakout)
bars_since_bearish = ta.barssince(bearish_breakout)

if bullish_breakout
    breakout_price := close
confirmed_bullish := bars_since_bullish <= breakout_confirmation and close > resistance

if bearish_breakout
    breakout_price := close
confirmed_bearish := bars_since_bearish <= breakout_confirmation and close < support

// Tjek om retningen skal ændres baseret på prisbevægelse
var bool allow_direction_change = false
if trade_direction != 0 and not na(active_entry)
    if trade_direction == 1  // Bullish handel
        if close <= active_entry * (1 - direction_change_threshold / 100)
            allow_direction_change := true
    else if trade_direction == -1  // Bearish handel
        if close >= active_entry * (1 + direction_change_threshold / 100)
            allow_direction_change := true

// Tjek om den aktive handel er afsluttet (rammer SL, TP1 eller TP2)
if trade_direction != 0 and not na(active_sl) and not na(active_tp1) and not na(active_tp2)
    if trade_direction == 1  // Bullish handel
        if low <= active_sl or high >= active_tp1 or high >= active_tp2
            trade_direction := 0
    else if trade_direction == -1  // Bearish handel
        if high >= active_sl or low <= active_tp1 or low <= active_tp2
            trade_direction := 0

// Beregn niveauer for TP og SL direkte ved breakout, hvis betingelserne er opfyldt
var float entry_price = na
var float stop_loss = na
var float take_profit_1 = na
var float take_profit_2 = na

if confirmed_bullish and not na(support) and not na(breakout_price) and (trade_direction == 0 or trade_direction == 1 or allow_direction_change)
    entry_price := close
    stop_loss := entry_price * (1 - sl_percent / 100)
    take_profit_1 := entry_price * (1 + tp1_percent / 100)
    take_profit_2 := entry_price * (1 + tp2_percent / 100)

    // Gem breakout-detaljer i arrays
    array.push(entry_prices, entry_price)
    array.push(entry_indices, bar_index)
    array.push(sl_prices, stop_loss)
    array.push(tp1_prices, take_profit_1)
    array.push(tp2_prices, take_profit_2)
    array.push(is_bullish_trades, true)

    // Sæt retning
    trade_direction := 1
    active_entry := entry_price
    active_sl := stop_loss
    active_tp1 := take_profit_1
    active_tp2 := take_profit_2

    // Slet tidligere linjer og labels, hvis vi kun viser det seneste entry
    if not show_multiple_entries
        if array.size(entry_lines) > 0
            for i = 0 to array.size(entry_lines) - 1
                line.delete(array.get(entry_lines, i))
                label.delete(array.get(entry_labels, i))
                line.delete(array.get(sl_lines, i))
                label.delete(array.get(sl_labels, i))
                line.delete(array.get(tp1_lines, i))
                label.delete(array.get(tp1_labels, i))
                line.delete(array.get(tp2_lines, i))
                label.delete(array.get(tp2_labels, i))
        array.clear(entry_lines)
        array.clear(entry_labels)
        array.clear(sl_lines)
        array.clear(sl_labels)
        array.clear(tp1_lines)
        array.clear(tp1_labels)
        array.clear(tp2_lines)
        array.clear(tp2_labels)

    // Tegn nye linjer og labels
    new_entry_line = line.new(bar_index, entry_price, bar_index + 50, entry_price, color=color.new(color.yellow, 20), style=line.style_dashed, width=1)
    new_entry_label = label.new(bar_index, entry_price, "Entry", color=color.new(color.yellow, 20), style=label.style_label_down, textcolor=color.white)
    new_sl_line = line.new(bar_index, stop_loss, bar_index + 50, stop_loss, color=color.new(color.red, 20), style=line.style_dashed, width=1)
    new_sl_label = label.new(bar_index, stop_loss, "SL", color=color.new(color.red, 20), style=label.style_label_up, textcolor=color.white)
    new_tp1_line = line.new(bar_index, take_profit_1, bar_index + 50, take_profit_1, color=color.new(color.green, 20), style=line.style_dashed, width=1)
    new_tp1_label = label.new(bar_index, take_profit_1, "TP1", color=color.new(color.green, 20), style=label.style_label_down, textcolor=color.white)
    new_tp2_line = line.new(bar_index, take_profit_2, bar_index + 50, take_profit_2, color=color.new(color.green, 20), style=line.style_dashed, width=1)
    new_tp2_label = label.new(bar_index, take_profit_2, "TP2", color=color.new(color.green, 20), style=label.style_label_down, textcolor=color.white)

    array.push(entry_lines, new_entry_line)
    array.push(entry_labels, new_entry_label)
    array.push(sl_lines, new_sl_line)
    array.push(sl_labels, new_sl_label)
    array.push(tp1_lines, new_tp1_line)
    array.push(tp1_labels, new_tp1_label)
    array.push(tp2_lines, new_tp2_line)
    array.push(tp2_labels, new_tp2_label)

    // Begræns til de sidste 5 entries, hvis vi viser flere
    if show_multiple_entries and array.size(entry_lines) > 5
        line.delete(array.shift(entry_lines))
        label.delete(array.shift(entry_labels))
        line.delete(array.shift(sl_lines))
        label.delete(array.shift(sl_labels))
        line.delete(array.shift(tp1_lines))
        label.delete(array.shift(tp1_labels))
        line.delete(array.shift(tp2_lines))
        label.delete(array.shift(tp2_labels))

    // Vis label for retningsændring, hvis det er relevant
    if allow_direction_change
        if not na(last_direction_change_label)
            label.delete(last_direction_change_label)
        last_direction_change_label := label.new(bar_index, high, "Direction Change (Bullish)", color=color.green, style=label.style_label_down, textcolor=color.white)

    alert("Bullish Breakout Confirmed! Entry: " + str.tostring(entry_price) + ", SL: " + str.tostring(stop_loss) + ", TP1: " + str.tostring(take_profit_1) + ", TP2: " + str.tostring(take_profit_2))

if confirmed_bearish and not na(resistance) and not na(breakout_price) and (trade_direction == 0 or trade_direction == -1 or allow_direction_change)
    entry_price := close
    stop_loss := entry_price * (1 + sl_percent / 100)
    take_profit_1 := entry_price * (1 - tp1_percent / 100)
    take_profit_2 := entry_price * (1 - tp2_percent / 100)

    // Gem breakout-detaljer i arrays
    array.push(entry_prices, entry_price)
    array.push(entry_indices, bar_index)
    array.push(sl_prices, stop_loss)
    array.push(tp1_prices, take_profit_1)
    array.push(tp2_prices, take_profit_2)
    array.push(is_bullish_trades, false)

    // Sæt retning
    trade_direction := -1
    active_entry := entry_price
    active_sl := stop_loss
    active_tp1 := take_profit_1
    active_tp2 := take_profit_2

    // Slet tidligere linjer og labels, hvis vi kun viser det seneste entry
    if not show_multiple_entries
        if array.size(entry_lines) > 0
            for i = 0 to array.size(entry_lines) - 1
                line.delete(array.get(entry_lines, i))
                label.delete(array.get(entry_labels, i))
                line.delete(array.get(sl_lines, i))
                label.delete(array.get(sl_labels, i))
                line.delete(array.get(tp1_lines, i))
                label.delete(array.get(tp1_labels, i))
                line.delete(array.get(tp2_lines, i))
                label.delete(array.get(tp2_labels, i))
        array.clear(entry_lines)
        array.clear(entry_labels)
        array.clear(sl_lines)
        array.clear(sl_labels)
        array.clear(tp1_lines)
        array.clear(tp1_labels)
        array.clear(tp2_lines)
        array.clear(tp2_labels)

    // Tegn nye linjer og labels
    new_entry_line = line.new(bar_index, entry_price, bar_index + 50, entry_price, color=color.new(color.yellow, 20), style=line.style_dashed, width=1)
    new_entry_label = label.new(bar_index, entry_price, "Entry", color=color.new(color.yellow, 20), style=label.style_label_down, textcolor=color.white)
    new_sl_line = line.new(bar_index, stop_loss, bar_index + 50, stop_loss, color=color.new(color.red, 20), style=line.style_dashed, width=1)
    new_sl_label = label.new(bar_index, stop_loss, "SL", color=color.new(color.red, 20), style=label.style_label_up, textcolor=color.white)
    new_tp1_line = line.new(bar_index, take_profit_1, bar_index + 50, take_profit_1, color=color.new(color.green, 20), style=line.style_dashed, width=1)
    new_tp1_label = label.new(bar_index, take_profit_1, "TP1", color=color.new(color.green, 20), style=label.style_label_down, textcolor=color.white)
    new_tp2_line = line.new(bar_index, take_profit_2, bar_index + 50, take_profit_2, color=color.new(color.green, 20), style=line.style_dashed, width=1)
    new_tp2_label = label.new(bar_index, take_profit_2, "TP2", color=color.new(color.green, 20), style=label.style_label_down, textcolor=color.white)

    array.push(entry_lines, new_entry_line)
    array.push(entry_labels, new_entry_label)
    array.push(sl_lines, new_sl_line)
    array.push(sl_labels, new_sl_label)
    array.push(tp1_lines, new_tp1_line)
    array.push(tp1_labels, new_tp1_label)
    array.push(tp2_lines, new_tp2_line)
    array.push(tp2_labels, new_tp2_label)

    // Begræns til de sidste 5 entries, hvis vi viser flere
    if show_multiple_entries and array.size(entry_lines) > 5
        line.delete(array.shift(entry_lines))
        label.delete(array.shift(entry_labels))
        line.delete(array.shift(sl_lines))
        label.delete(array.shift(sl_labels))
        line.delete(array.shift(tp1_lines))
        label.delete(array.shift(tp1_labels))
        line.delete(array.shift(tp2_lines))
        label.delete(array.shift(tp2_labels))

    // Vis label for retningsændring, hvis det er relevant
    if allow_direction_change
        if not na(last_direction_change_label)
            label.delete(last_direction_change_label)
        last_direction_change_label := label.new(bar_index, low, "Direction Change (Bearish)", color=color.red, style=label.style_label_up, textcolor=color.white)

    alert("Bearish Breakout Confirmed! Entry: " + str.tostring(entry_price) + ", SL: " + str.tostring(stop_loss) + ", TP1: " + str.tostring(take_profit_1) + ", TP2: " + str.tostring(take_profit_2))

// Tegn signaler for breakout og fjern tidligere labels
if bullish_breakout
    if array.size(breakout_labels) > 0
        label.delete(array.get(breakout_labels, array.size(breakout_labels) - 1))
    array.push(breakout_labels, label.new(bar_index, high, "Breakout", color=color.green, style=label.style_label_down, textcolor=color.white))
    // Begræns til de sidste 5 breakout-labels
    if array.size(breakout_labels) > 5
        label.delete(array.shift(breakout_labels))

if bearish_breakout
    if array.size(breakout_labels) > 0
        label.delete(array.get(breakout_labels, array.size(breakout_labels) - 1))
    array.push(breakout_labels, label.new(bar_index, low, "Breakout", color=color.red, style=label.style_label_up, textcolor=color.white))
    // Begræns til de sidste 5 breakout-labels
    if array.size(breakout_labels) > 5
        label.delete(array.shift(breakout_labels))